home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / AmiVoGL_MDEV.lha / src / trans.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  5KB  |  197 lines

  1. #include "vogl.h"
  2.  
  3. #ifdef    TC
  4.  
  5. extern    double    cos();
  6. extern    double    sin();
  7.  
  8. #else 
  9.  
  10. #include <math.h>
  11.  
  12. #endif
  13.  
  14. /*
  15.  * translate
  16.  * 
  17.  * Set up a translation matrix and premultiply it and 
  18.  * the top matrix on the stack.
  19.  *
  20.  */
  21. void translate(
  22.   float x,
  23.   float y,
  24.   float z)
  25. {
  26. Token    *tok;
  27.  
  28. if (!vdevice.initialised) 
  29. verror("translate: vogl not initialised");
  30.  
  31.         if (vdevice.inobject) {
  32.     tok = newtokens(4);
  33.  
  34.     tok[0].i = TRANSLATE;
  35.     tok[1].f = x;
  36.     tok[2].f = y;
  37.     tok[3].f = z;
  38.  
  39.                     return;
  40.             }
  41.  
  42. /*
  43.  * Do the operations directly on the top matrix of
  44.  * the stack to speed things up.
  45.  */
  46. vdevice.transmat->m[3][0] += x * vdevice.transmat->m[0][0]
  47.    + y * vdevice.transmat->m[1][0]
  48.    + z * vdevice.transmat->m[2][0];
  49.  
  50. vdevice.transmat->m[3][1] += x * vdevice.transmat->m[0][1]
  51.    + y * vdevice.transmat->m[1][1]
  52.    + z * vdevice.transmat->m[2][1];
  53.  
  54. vdevice.transmat->m[3][2] += x * vdevice.transmat->m[0][2]
  55.    + y * vdevice.transmat->m[1][2]
  56.    + z * vdevice.transmat->m[2][2];
  57.  
  58. vdevice.transmat->m[3][3] += x * vdevice.transmat->m[0][3]
  59.    + y * vdevice.transmat->m[1][3]
  60.    + z * vdevice.transmat->m[2][3];
  61. }
  62.  
  63. /* ------------------------------------------------------------------------ */
  64.  
  65. /*
  66.  * rot
  67.  * 
  68.  * Set up a rotate matrix and premultiply it with 
  69.  * the top matrix on the stack.
  70.  *
  71.  */
  72. void rot(
  73.   float r,
  74.   char axis)
  75. {
  76. Token        *tok;
  77. register float    costheta, sintheta, tmp;
  78.  
  79. if (!vdevice.initialised)
  80. verror("rot: vogl not initialised");
  81.  
  82.         if (vdevice.inobject) {
  83.     tok = newtokens(3);
  84.  
  85.     tok[0].i = ROTATE;
  86.     tok[1].f = r;
  87.     tok[2].i = axis;
  88.  
  89.                     return;
  90.             }
  91.  
  92. /*
  93.  * Do the operations directly on the top matrix of
  94.  * the stack to speed things up.
  95.  */
  96. costheta = cos((double)(D2R * r));
  97. sintheta = sin((double)(D2R * r));
  98.  
  99. switch(axis) {
  100. case 'x':
  101. case 'X':
  102.     tmp = vdevice.transmat->m[1][0];
  103.     vdevice.transmat->m[1][0] = costheta * tmp
  104.       + sintheta * vdevice.transmat->m[2][0];
  105.     vdevice.transmat->m[2][0] = costheta * vdevice.transmat->m[2][0]
  106.       - sintheta * tmp;
  107.  
  108.     tmp = vdevice.transmat->m[1][1];
  109.     vdevice.transmat->m[1][1] = costheta * tmp
  110.       + sintheta * vdevice.transmat->m[2][1];
  111.     vdevice.transmat->m[2][1] = costheta * vdevice.transmat->m[2][1]
  112.       - sintheta * tmp;
  113.     tmp = vdevice.transmat->m[1][2];
  114.     vdevice.transmat->m[1][2] = costheta * tmp
  115.       + sintheta * vdevice.transmat->m[2][2];
  116.     vdevice.transmat->m[2][2] = costheta * vdevice.transmat->m[2][2]
  117.       - sintheta * tmp;
  118.  
  119.     tmp = vdevice.transmat->m[1][3];
  120.     vdevice.transmat->m[1][3] = costheta * tmp
  121.       + sintheta * vdevice.transmat->m[2][3];
  122.     vdevice.transmat->m[2][3] = costheta * vdevice.transmat->m[2][3]
  123.       - sintheta * tmp;
  124.     break;
  125. case 'y':
  126. case 'Y':
  127.     tmp = vdevice.transmat->m[0][0];
  128.     vdevice.transmat->m[0][0] = costheta * tmp
  129.       - sintheta * vdevice.transmat->m[2][0];
  130.     vdevice.transmat->m[2][0] = sintheta * tmp
  131.       + costheta * vdevice.transmat->m[2][0];
  132.     tmp = vdevice.transmat->m[0][1];
  133.     vdevice.transmat->m[0][1] = costheta * tmp
  134.       - sintheta * vdevice.transmat->m[2][1];
  135.     vdevice.transmat->m[2][1] = sintheta * tmp
  136.       + costheta * vdevice.transmat->m[2][1];
  137.     tmp = vdevice.transmat->m[0][2];
  138.     vdevice.transmat->m[0][2] = costheta * tmp
  139.       - sintheta * vdevice.transmat->m[2][2];
  140.     vdevice.transmat->m[2][2] = sintheta * tmp
  141.       + costheta * vdevice.transmat->m[2][2];
  142.     tmp = vdevice.transmat->m[0][3];
  143.     vdevice.transmat->m[0][3] = costheta * tmp
  144.       - sintheta * vdevice.transmat->m[2][3];
  145.     vdevice.transmat->m[2][3] = sintheta * tmp
  146.       + costheta * vdevice.transmat->m[2][3];
  147.     break;
  148. case 'z':
  149. case 'Z':
  150.     tmp = vdevice.transmat->m[0][0];
  151.     vdevice.transmat->m[0][0] = costheta * tmp
  152.       + sintheta * vdevice.transmat->m[1][0];
  153.     vdevice.transmat->m[1][0] = costheta * vdevice.transmat->m[1][0]
  154.       - sintheta * tmp;
  155.  
  156.     tmp = vdevice.transmat->m[0][1];
  157.     vdevice.transmat->m[0][1] = costheta * tmp
  158.       + sintheta * vdevice.transmat->m[1][1];
  159.     vdevice.transmat->m[1][1] = costheta * vdevice.transmat->m[1][1]
  160.       - sintheta * tmp;
  161.  
  162.     tmp = vdevice.transmat->m[0][2];
  163.     vdevice.transmat->m[0][2] = costheta * tmp
  164.       + sintheta * vdevice.transmat->m[1][2];
  165.     vdevice.transmat->m[1][2] = costheta * vdevice.transmat->m[1][2]
  166.       - sintheta * tmp;
  167.  
  168.     tmp = vdevice.transmat->m[0][3];
  169.     vdevice.transmat->m[0][3] = costheta * tmp
  170.       + sintheta * vdevice.transmat->m[1][3];
  171.     vdevice.transmat->m[1][3] = costheta * vdevice.transmat->m[1][3]
  172.       - sintheta * tmp;
  173.     break;
  174. default:
  175.     verror("rot: illegal axis of rotation");
  176.     }
  177. }
  178.  
  179. /* ------------------------------------------------------------------------ */
  180.  
  181. /*
  182.  * rotate
  183.  * 
  184.  * Set up an old style, I've got this real fast way of doing
  185.  * it providing I use ints, rotate.
  186.  *
  187.  */
  188. void rotate(
  189.   Angle r,
  190.   char axis)
  191. {
  192. rot(r / (float)10, axis);
  193. }
  194.  
  195. /* ------------------------------------------------------------------------ */
  196.  
  197.